﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using System.Threading.Tasks;

namespace Battle
{
    public static class BulletFactory 
    {
        public const string NormalPath = "Bullet";
        public const string ElectricityPath = "Bullet_Electricity";
        public const string AOEPath = "Bullet_Aoe";
        public const string RayPath = "Bullet_Ray";

        public static async Task<BulletBase> CreateNormalBulletAsync(ST_BulletData bulletData, IBulletHitHandler hitHandler = null)
        {
            return await CreateBulletAsync<BulletBase>(bulletData, NormalPath, hitHandler);
        }

        public static async Task<T> CreateBulletAsync<T>(ST_BulletData bulletData, string prefabPath = null, IBulletHitHandler hitHandler = null, IEnemyTargetGetter targetGetter = null)
            where T : MonoBehaviour, IBullet
        {
            if (string.IsNullOrEmpty(prefabPath)) prefabPath = NormalPath;

            GameObject prefab = await Addressables.LoadAssetAsync<GameObject>(prefabPath).Task;

            GameObject obj = MonoBehaviour.Instantiate(prefab);

            T bullet = obj.GetComponent<T>();
            if (bullet == null)
                bullet = obj.AddComponent<T>();

            //不指定获取器就给个默认的
            bullet.InitBulletData(bulletData, hitHandler, targetGetter ?? EnemyMgr.Ins);
            return bullet;
        }
    }
}